import axios from 'axios'
import Assert from "../common/Assert.js";

export default class CaseLawService {
    static caseLawService = null
    requestUrl = 'http://localhost/laws'

    static getInstance() {
        if (this.caseLawService == null) {
            this.caseLawService = new CaseLawService()
        }
        return this.caseLawService
    }

    list(params) {
        return axios.get(this.requestUrl, params)
    }


    /**
     * 根据身份证检查
     * @param params
     * @returns {Promise<AxiosResponse<T>>}
     */
    getByIdCard(params) {
        return axios.get(this.requestUrl + "/card", {
            params: params
        })
    }

    selectByCaseLaw(wid) {
        return axios.get(this.requestUrl + `/selectByCaseLaw/${wid}`)
    }

    /**
     * 获取最大编号
     * @returns {Promise<AxiosResponse<T>>}
     */
    getCode() {
        return axios.get(this.requestUrl + '/code')
    }

    check(vue, law, cwPerformancesplitList) {
        //验证数据是否合法
        if (!Assert.isNotNull(law.caseCause, '请输入案由')) {
            return false
        }
        if (Assert.isNotNull(law.caseOppositeCardnum)) {
            if (!Assert.isNotNull(law.caseOppositeParties, '请输入对方当事人')) {
                return false
            }
        }
        if (Assert.isNotNull(law.caseOppositeCardnum)) {
            if (!Assert.validIdCard(law.caseOppositeCardnum)) {
                return false
            }
        }
        if (!Assert.isNotNull(law.collectionTime, '请输入收案时间')) {
            return false
        }
        if (!Assert.isNotNull(law.caseChargeWay, '请选择收费方式')) {
            return false
        }
        if (!Assert.isNotNull(law.caseAgencyfee, '请输入代理费') || !Assert.isLessZero(law.caseAgencyfee, '请输入合法的代理费')) {
            return false
        }
        if (!Assert.isNotNull(law.caseFxmoney, '请输入风险费') || !Assert.isLessZero(law.caseFxmoney, '请输入合法的风险费')) {
            return false
        }
        if (!Assert.isNotNull(law.caseFxmoney, '请输入补助金额') || !Assert.isLessZero(law.caseSubsidysal, '请输入合法的补助金额')) {
            return false
        }
        if (!Assert.isNotNull(law.caseStatus, '请选择诉讼阶段')) {
            return false
        }
        if (!Assert.isNotNull(law.caseLawsuitStatus, '请选择诉讼地位')) {
            return false
        }
        //判断填的分成是否合法以及是否选完整了律师
        let totalMoney = 0
        //保存律师字段值得变量
        let attorney = []
        try {
            cwPerformancesplitList.forEach(legal => {
                //判断是百分比分成还是固定分成
                if (legal.cwSplitType == '百分比') {
                    legal.cwRealSplitMoney = law.caseAgencyfee * (legal.cwPerformancesplit - 0) / 100
                } else {
                    legal.cwRealSplitMoney = legal.cwPerformancesplit - 0
                }
                totalMoney += legal.cwRealSplitMoney
                //验证是否选了用户
                if (!Assert.isNotNull(legal.empName, '请选择律师')) {
                    throw new Error()
                }
                //验证输入的数字是否合法
                if (!Assert.isNotNull(legal.cwPerformancesplit, '请输入合法的分成') || !Assert.isLessZero(legal.cwPerformancesplit, '请输入合法的分成')) {
                    throw new Error()
                }
                //验证是否有重复的人
                let count = 0
                cwPerformancesplitList.forEach(item => {
                    if (item.hrEmpId == legal.hrEmpId) {
                        count++
                    }
                })
                if (count !== 1) {
                    vue.$message.warning({
                        message: '存在重复的人',
                        duration: 1000
                    })
                    throw new Error()
                }
                //拼接办案律师
                attorney.push(legal.caseAttorney + ":" + legal.empName)
            })
        } catch (e) {
            return false
        }
        //验证总金额是否大于代理费
        if (totalMoney > law.caseAgencyfee) {
            vue.$message.warning({
                message: '业绩分成不能大于代理费总额',
                duration: 1000
            })
            return false
        }

        //设置律师字段到对象中
        law.hrEmpId = cwPerformancesplitList[0].hrEmpId
        law.caseAttorney = attorney.join(',')
        law.CaseSubmitter = sessionStorage.getItem('realName')
        return true
    }

    zxdlCheck(vue, law, cwPerformancesplitList) {
        //验证数据是否合法
        if (!Assert.isNotNull(law.caseCause, '请输入咨询事项')) {
            return false
        }
        if (!Assert.isNotNull(law.collectionTime, '请选择服务时间')) {
            return false
        }
        if (!Assert.isNotNull(law.caseAgencyfee, '请输入服务费用') || !Assert.isLessZero(law.caseAgencyfee, '请输入合法的服务费用')) {
            return false
        }
        if (!Assert.isNotNull(law.caseWtr,'请输入委托人')) {
            return false
        }
        if (Assert.isNotNull(law.standard)) {
            if (!Assert.isLessZero(law.standard, '请输入合法的人次')) {
                return false
            }
        }
        //判断填的分成是否合法以及是否选完整了律师
        let totalMoney = 0
        //保存律师字段值得变量
        let attorney = []
        try {
            cwPerformancesplitList.forEach(legal => {
                //判断是百分比分成还是固定分成
                if (legal.cwSplitType == '百分比') {
                    legal.cwRealSplitMoney = law.caseAgencyfee * (legal.cwPerformancesplit - 0) / 100
                } else {
                    legal.cwRealSplitMoney = legal.cwPerformancesplit - 0
                }
                totalMoney += legal.cwRealSplitMoney
                //验证是否选了用户
                if (!Assert.isNotNull(legal.empName, '请选择律师')) {
                    throw new Error()
                }
                //验证输入的数字是否合法
                if (!Assert.isNotNull(legal.cwPerformancesplit, '请输入合法的分成') || !Assert.isLessZero(legal.cwPerformancesplit, '请输入合法的分成')) {
                    throw new Error()
                }
                //验证是否有重复的人
                let count = 0
                cwPerformancesplitList.forEach(item => {
                    if (item.hrEmpId == legal.hrEmpId) {
                        count++
                    }
                })
                if (count !== 1) {
                    vue.$message.warning({
                        message: '存在重复的人',
                        duration: 1000
                    })
                    throw new Error()
                }
                //拼接办案律师
                attorney.push(legal.caseAttorney + ":" + legal.empName)
            })
        } catch (e) {
            return false
        }
        //验证总金额是否大于代理费
        if (totalMoney > law.caseAgencyfee) {
            vue.$message.warning({
                message: '业绩分成不能大于代理费总额',
                duration: 1000
            })
            return false
        }

        //设置律师字段到对象中
        law.hrEmpId = cwPerformancesplitList[0].hrEmpId
        law.caseAttorney = attorney.join(',')
        law.CaseSubmitter = sessionStorage.getItem('realName')
        return true
    }

    /**
     * 根据id查询案件的方法
     * @param id
     */
    getById(id) {
        return axios.get(`${this.requestUrl}/${id}`)
    }

    /**
     * 新增案件的方法
     * @param caseLaw
     * @param cwPerformancesplitList
     * @returns {Promise<AxiosResponse<T>>}
     */
    save(caseLaw, cwPerformancesplitList) {
        const params = {
            caseLaw, cwPerformancesplitList
        }
        return axios.post(this.requestUrl, params)
    }

    /**
     * 分页查询
     * @param params
     */
    getByPage(params) {
        return axios.get(`${this.requestUrl}/page`, {
            params
        })
    }

    /**
     * 修改案件加办案律师的方法
     * @param caseLaw
     * @param cwPerformancesplitList
     */
    updateWithlawyer(caseLaw, cwPerformancesplitList) {
        const params = {
            caseLaw, cwPerformancesplitList
        }
        return axios.put(`${this.requestUrl}/lawyer`, params)
    }

    /**
     * 修改的方法
     * @param law
     */
    update(law) {
        return axios.put(this.requestUrl, law)
    }

    /**
     * 批量更新
     * @param laws
     */
    updateBatch(laws) {
        return axios.put(`${this.requestUrl}/total`, laws)
    }

    /**
     * 结案的方法
     * @param law
     */
    caseClose(law) {
        return axios.put(`${this.requestUrl}/close`, law)
    }

    /**
     *
     */
    selectByForm(param) {
        return axios.get(`${this.requestUrl}/form`, {
            params: param
        })
    }

    /**
     * 收结案统计
     */
    statisticsList(params) {
        return axios.get("http://localhost/settleCase", {
            params
        })
    }

    /**
     * 导出
     */
    dc(params) {

        return axios.get("http://localhost/settleCase/dcExcel", {
            responseType: 'blob',
            params: params
        })
    }

    /**
     * 顾问统计
     */
    dlfCount(params) {
        return axios.get("http://localhost/laws/dlfCount", {
            params
        })
    }

    /**
     * 开票总汇统计
     */
    kpCount(params) {
        return axios.get("http://localhost/caseTypes/typeInvoice", {
            params
        })
    }
}
